package handler

import (
	"oa-system/internal/api/request"
	"oa-system/internal/service"
	"oa-system/pkg/errors"
	"oa-system/pkg/response"
	"oa-system/pkg/utils"

	"github.com/gin-gonic/gin"
)

type DictionaryHandler struct {
	dictService service.DictionaryService
}

func NewDictionaryHandler(dictService service.DictionaryService) *DictionaryHandler {
	return &DictionaryHandler{
		dictService: dictService,
	}
}

// GetAllDictionaries godoc
// @Summary 获取字典列表
// @Description 获取所有字典信息，支持分页和搜索
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param page query int false "页码" default(1)
// @Param pageSize query int false "每页数量" default(10)
// @Param type query string false "字典类型"
// @Param label query string false "字典标签"
// @Success 200 {object} response.Response{data=response.PageData{list=[]model.Dictionary,total=int}}
// @Router /dicts [get]
func (h *DictionaryHandler) GetAllDictionaries(c *gin.Context) {
	var req request.PageRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, errors.NewBadRequestError("无效的请求参数", err))
		return
	}

	typeSearch := c.Query("type")
	labelSearch := c.Query("label")

	dictionaries, total, err := h.dictService.GetAllDictionaries(req.Page, req.PageSize, typeSearch, labelSearch)
	if err != nil {
		response.Error(c, err)
		return
	}

	response.PageSuccess(c, dictionaries, total, req.Page, req.PageSize)
}

// CreateDictionary godoc
// @Summary 创建字典
// @Description 创建新的字典
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param dictionary body request.CreateDictionaryRequest true "字典信息"
// @Success 200 {object} response.Response{data=model.Dictionary}
// @Router /dicts [post]
func (h *DictionaryHandler) CreateDictionary(c *gin.Context) {
	var req request.CreateDictionaryRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, errors.NewBadRequestError("无效的请求参数", err))
		return
	}

	dictionary, err := h.dictService.CreateDictionary(&req)
	if err != nil {
		response.Error(c, err)
		return
	}

	response.Success(c, dictionary)
}

// UpdateDictionary godoc
// @Summary 更新字典
// @Description 更新指定字典的信息
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param id path uint true "字典ID"
// @Param dictionary body request.UpdateDictionaryRequest true "字典信息"
// @Success 200 {object} response.Response{data=model.Dictionary}
// @Router /dicts/{id} [put]
func (h *DictionaryHandler) UpdateDictionary(c *gin.Context) {
	id, err := utils.ParseUintParam(c, "id")
	if err != nil {
		response.Error(c, errors.NewBadRequestError("无效的字典ID", err))
		return
	}

	var req request.UpdateDictionaryRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, errors.NewBadRequestError("无效的请求参数", err))
		return
	}

	dictionary, err := h.dictService.UpdateDictionary(id, &req)
	if err != nil {
		response.Error(c, err)
		return
	}

	response.Success(c, dictionary)
}

// DeleteDictionary godoc
// @Summary 删除字典
// @Description 删除指定的字典
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param id path uint true "字典ID"
// @Success 200 {object} response.Response
// @Router /dicts/{id} [delete]
func (h *DictionaryHandler) DeleteDictionary(c *gin.Context) {
	id, err := utils.ParseUintParam(c, "id")
	if err != nil {
		response.Error(c, errors.NewBadRequestError("无效的字典ID", err))
		return
	}

	if err := h.dictService.DeleteDictionary(id); err != nil {
		response.Error(c, err)
		return
	}

	response.SuccessWithMessage(c, "删除成功", nil)
}

// GetDictionariesByType godoc
// @Summary 获取指定类型的字典列表
// @Description 获取指定类型的所有字典
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param type path string true "字典类型"
// @Success 200 {object} response.Response{data=[]model.Dictionary}
// @Router /dicts/type/{type} [get]
func (h *DictionaryHandler) GetDictionariesByType(c *gin.Context) {
	dictType := c.Param("type")
	if dictType == "" {
		response.Error(c, errors.NewBadRequestError("字典类型不能为空", nil))
		return
	}

	dictionaries, err := h.dictService.GetDictionariesByType(dictType)
	if err != nil {
		response.Error(c, err)
		return
	}

	response.Success(c, dictionaries)
}
